<?php
/**
 * 应用系统菜单管理
 * @author tangbl
 *
 */
class MenuAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 添加菜单
     * @access public
     * @param  int    $nodeid  节点id
     * @param  int    $key     菜单标识
     * @param  string $title   菜单名称
     * @param  int    $pid     父菜单
     * @param  int    $type    类型[1菜单|2快捷方式]
     * @param  string $icon    菜单图标
     * @return array
     */
    public function add(){
    	
    	$param = array(
    		"before" => "menu_deal_before"
    	);
    	parent::do_add("Menu",$param);
    }
    
    /**
     * 修改角色
     * @access public
     * @param  int    $nodeid  节点id
     * @param  int    $key     菜单标识
     * @param  string $title   菜单名称
     * @param  int    $pid     父菜单
     * @param  int    $type    类型[1菜单|2快捷方式]
     * @param  string $icon    菜单图标
     * @return array
     */
    public function edit(){
    	 
    	$param = array(
    		"before" => "menu_deal_before"
    	);
    	parent::do_edit("Menu",$param);
    }
    
    /**
     * 查询菜单
     * @access public
     * @param  int    $company 所属商户
     * @return array
     */
    public function index(){
		
    	$model = D("Menu");
    	$data  = $model->listMenu($map);
    	
    	$this->success($data);
    }
    
    /**
     * 根据对应的条件查询菜单
     * @access public
     * @param  int    $id  菜单id
     * @return array
     */
    public function find(){
    
    	$id = $this->_get("id","intval",0);
    
    	$model = D("Menu");
    	$map   = array("id"=>$id);
    	$data  = $model->where($map)->find();
    
    	$this->success($data);
    }
    
    /**
     * 根据应用系统角色查询对应的菜单
     * @access public
     * @param  int    $role 角色id
     * @return array
     */
    public function listByRole(){
    
    	$map['role'] = $this->_get("role","intval",0);
    
    	$model = D("MenuRoleView");
    	$data  = $model->where($map)->select();
    	 
    	$this->success($data);
    }
    
    /**
     * 删除菜单
     * @access public
     * @param  int    $id  菜单编号
     * @return array
     */
    public function del(){
    	
    	$id     = $this->_get("id","intval",0);
    	
    	$model  = D("Menu");
    	$result = $model->delMenu($id);
    	
    	if($result){
    		$this->success($result,"删除菜单成功");
    	}else{
    		$this->error("删除菜单失败");
    	}
    }
    
    /**
     * 排序菜单
     * @access public
     * @param  array  $sort 排序数组
     * @return array
     */
    public function sort(){
    
    	$sort   = $this->_req("sort");
    	$model  = D("Menu");
    	$result = 0; 
    	foreach($sort as $k=>$v){
    		$result += $model->where("id=".$k)->save(array("sort"=>$v));
    	}
    
    	$this->success($result,"排序成功");
    }
    
    /**
     * 根据角色对应用户的快捷方式
     * @access public
     * @param  int    $role   角色id
     * @param  int    $userid 用户id
     * @return array
     */
    public function quickByUser(){
    	
    	$role   = $this->_req("role"  ,"intval",0);
		$userid = $this->_req("userid","intval",0);
		
		$field  = "id,nodeid,pid,title,level,sort,url,type";
		$order  = "sort desc";
	
		if($role && $userid){
			
			$map    = array("role"=>$role,"type"=>2);
			$model  = D("MenuRoleView");
			$menus  = $model->where($map)->field($field)->order($order)->select();
			//echo $model->getLastSql();
			$order  = "orderno desc";
			$map    = array("user_id"=>$userid);
			$model  = M("AdminMenuUser");
			$nodes  = $model->where($map)->getField("menu_id,orderno");
			$flag   = true;
			if(empty($nodes)){
				$flag = false;
			}
			
			foreach($menus as $k=>$v){
				
				$v['checked'] = 1;
				$v['orderno'] = $v['sort'];
				$id = $v["id"];
				
				$exists = array_key_exists($id, $nodes);
				if($flag && !$exists){
					$v['checked'] = 0;
				}else{
					if($exists){
						$v['orderno'] = $nodes[$id];
					}
				}
				
				$rows[] = $v;
			}
			
			$order =array();
			foreach ($rows as $k=>$row) {
				$order[$k] = $row['orderno'];
			}
			array_multisort($order, SORT_DESC, $rows);
			$this->success($rows);
		}else{
			$this->error("");	
		}
    }
    
    /**
     * 修改用户的快捷方式
     * @access public
     * @param  int    $role   角色id
     * @param  int    $userid 用户id
     * @return array
     */
    public function quickUserEdit(){
    
    	$role   = $this->_req("role");
    	$userid = $this->_req("userid");

    	if($role && $userid){
			
    		$menu = $_POST["menu"];
    		$sort = $_POST["sort"];
    		
    		$map  = array("user_id"=>$userid);
    		$model= M("AdminMenuUser");
    		$model->where($map)->delete();
    		
    		foreach($menu AS $k=>$v){
    			$data['user_id'] = $userid;
    			$data['menu_id'] = $k;
    			$data['orderno'] = $sort[$k];
    			$model->add($data);
    		}
    		
    	}else{
    		$this->error("修改失败");
    	}
    }
}